package com.mflex.WHSmartWhouseAd.activity;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;

import com.mflex.WHSmartWhouseAd.R;
import com.mflex.WHSmartWhouseAd.adapter.SMTMaterialOutStockTaskDetailAdapter;
import com.mflex.WHSmartWhouseAd.adapter.StockTaskDetailAdapter;
import com.mflex.WHSmartWhouseAd.adapter.StockTaskDetaillistAdapter;
import com.mflex.WHSmartWhouseAd.model.ActionPdaInventoryTaskDto;
import com.mflex.WHSmartWhouseAd.model.MaterialDetailForSMTModel;
import com.mflex.WHSmartWhouseAd.model.ResCheckFatherAndSonDto;
import com.mflex.WHSmartWhouseAd.model.WHCompCheckTaskDetails;
import com.mflex.WHSmartWhouseAd.model.WHINVTaskResult;
import com.mflex.WHSmartWhouseAd.utils.HideKeyBoardUtil;
import com.xuexiang.xhttp2.XHttp;
import com.xuexiang.xhttp2.callback.SimpleCallBack;
import com.xuexiang.xhttp2.exception.ApiException;
import com.xuexiang.xhttp2.reflect.TypeToken;
import com.xuexiang.xui.utils.WidgetUtils;
import com.xuexiang.xui.widget.dialog.MiniLoadingDialog;
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog;
import com.xuexiang.xui.widget.textview.supertextview.SuperButton;
import com.xuexiang.xutil.common.StringUtils;
import com.xuexiang.xutil.net.JsonUtil;

import java.lang.reflect.Type;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class StockTaskListDetaillistActivity extends BaseActivity {

    EditText stvBoxNo;

    EditText stvMPN;

    EditText stvPanleName;

    EditText stvTotalVehQty;
    EditText stvScanVehQty;

    SuperButton buttonSave;
    ListView lvTaskList;
//    SuperButton buttonClear;

    MiniLoadingDialog mMiniLoadingDialog;

    private String userName;

    private String ID;
    private String pn;

    private int ScanQty;
    private int TotalQty;

    private String TaskID;
    private List<WHCompCheckTaskDetails> whCompCheckTaskDetailsList;

    private List<ResCheckFatherAndSonDto> WaitCheckSonList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stock_task_list_detail_list);

        mMiniLoadingDialog = WidgetUtils.getMiniLoadingDialog(this, "加载中...");

        stvBoxNo=findViewById(R.id.stv_BoxNo);
        stvMPN=findViewById(R.id.stv_MPN);
        stvPanleName=findViewById(R.id.stv_PanleName);

        buttonSave=findViewById(R.id.button_save);
        lvTaskList=findViewById(R.id.lvTaskList);


        stvTotalVehQty=findViewById(R.id.stvTotalVehQty);
        stvScanVehQty=findViewById(R.id.stvScanVehQty);


        Intent intent = getIntent();
        userName = intent.getStringExtra("userName");
        ID = intent.getStringExtra("Reelid");

        pn= intent.getStringExtra("MPN");


        ScanQty=Integer.parseInt(intent.getStringExtra("Qty"));
        TotalQty= Integer.parseInt(intent.getStringExtra("TotalQty"));

        TaskID= intent.getStringExtra("TaskID");

        stvBoxNo.setText(ID);
        stvMPN.setText(pn);

        stvTotalVehQty.setText(String.valueOf(TotalQty));
        stvScanVehQty.setText(String.valueOf(ScanQty));

        Type listType = new TypeToken<List<ResCheckFatherAndSonDto>>() {
        }.getType();
        WaitCheckSonList = (List<ResCheckFatherAndSonDto>) JsonUtil.fromJson(intent.getStringExtra("list"), listType);
        StockTaskDetaillistAdapter StockTaskDetailAdapter = new StockTaskDetaillistAdapter(StockTaskListDetaillistActivity.this, WaitCheckSonList);
        lvTaskList.setAdapter(StockTaskDetailAdapter);

        stvBoxNo.setOnKeyListener(this);
        stvMPN.setOnKeyListener(this);
        stvPanleName.setOnKeyListener(this);
        buttonSave.setOnClickListener(this);
//        buttonClear.setOnClickListener(this);


        stvPanleName.setFocusable(true);
        stvPanleName.setFocusableInTouchMode(true);
        stvPanleName.requestFocus();
        HideKeyBoardUtil.disableShowSoftInput(stvPanleName);
        stvPanleName.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b){
                    HideKeyBoardUtil.hideKeyBoard(StockTaskListDetaillistActivity.this);
                }
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.button_save:
                clearButtonMd(view);
                break;

            default:
                break;
        }
    }

    @Override
    public boolean onKey(View view, int i, KeyEvent keyEvent) {
        if (keyEvent.getAction() == KeyEvent.ACTION_DOWN && (i == 66 || i == 61)) {
            switch (view.getId()) {
                case R.id.stv_PanleName:
                    chekPanl();
                    break;

                default:
                    break;
            }


        }
        return false;
    }
    private void clearButtonMd(View view) {
//        stvBoxId.setText("");
//        stvWHLocName.setText("");
//        stvWHLocName.requestFocus();

        setResult(RESULT_OK);
        finish();

    }
    private void chekPanl() {
        if (StringUtils.isEmpty(stvPanleName.getText().toString())) {
            return;
        }
        mMiniLoadingDialog.show();
        Map<String, String> dictionary = new HashMap<>();

        // 向字典中添加键值对
        dictionary.put("TASKID", TaskID);
        dictionary.put("REELID", stvBoxNo.getText().toString());
        dictionary.put("SONID", stvPanleName.getText().toString());
        dictionary.put("USER", userName);

        XHttp.post("api/WHSmartWhousePDA/CheckFatherAndSon")
                .upJson(JsonUtil.toJson(dictionary))
                .syncRequest(false)
                .onMainThread(true)
                .accessToken(true)
                .timeOut(120000)
                .execute(new SimpleCallBack<ResCheckFatherAndSonDto>() {
                    @Override
                    public void onSuccess(ResCheckFatherAndSonDto response) {
                        mMiniLoadingDialog.dismiss();


                        for (ResCheckFatherAndSonDto model : WaitCheckSonList) {
                            if (StringUtils.equals(response.getSONID().toString(), model.getSONID().toString())) {
                                model.setSTATUS("2");
                            }
                        }
                        Collections.sort(WaitCheckSonList, new Comparator<ResCheckFatherAndSonDto>() {
                            @Override
                            public int compare(ResCheckFatherAndSonDto t0, ResCheckFatherAndSonDto t1) {
                                if (!StringUtils.equals("2", t0.getSTATUS()) && StringUtils.equals("2", t1.getSTATUS())) {
                                    return -1;
                                }
                                return 0;

                            }
                        });
                        int CheckStatus=0;
                        for (ResCheckFatherAndSonDto model:WaitCheckSonList) {
                            if (StringUtils.equals(response.getSONID().toString(), model.getSONID().toString())) {
                                if (!StringUtils.equals(model.getSTATUS(), "2")) {
                                    CheckStatus = 1;

                                } else {
                                    ScanQty = ScanQty + Integer.parseInt(model.getSONQTY());
                                }
                            }
                        }
                        if (CheckStatus==0){

                        }else{
                            StockTaskDetaillistAdapter stockTaskDetailAdapter = new StockTaskDetaillistAdapter(StockTaskListDetaillistActivity.this, WaitCheckSonList);
                            lvTaskList.setAdapter(stockTaskDetailAdapter);
                        }
                        stvScanVehQty.setText(String.valueOf(ScanQty));

                        Collections.sort(WaitCheckSonList, new Comparator<ResCheckFatherAndSonDto>() {
                            @Override
                            public int compare(ResCheckFatherAndSonDto t0, ResCheckFatherAndSonDto t1) {
                                if (!StringUtils.equals("2", t0.getSTATUS()) && StringUtils.equals("2", t1.getSTATUS())) {
                                    return -1;
                                }
                                return 0;

                            }
                        });
                        stvPanleName.setText("");
                        stvPanleName.requestFocus();
                    }

                    @Override
                    public void onError(ApiException e) {
                        stvPanleName.setText("");
                        stvPanleName.requestFocus();
                        mMiniLoadingDialog.dismiss();
                        new MaterialDialog.Builder(StockTaskListDetaillistActivity.this)
                                .iconRes(R.drawable.icon_warning)
                                .title(R.string.tip_warning)
                                .content(e.getMessage())
                                .positiveText(R.string.lab_submit)
                                .show();
                    }
                });
    }
}